<template>
	<ynet-content id="authModelMain" :headerInformation="true" :headerMenu="true">   
		<div class="main-container">
			<ynet-breadcrumb></ynet-breadcrumb>
		</div>
        <div class="main-container" >
            <div class="mt20">
                <ynet-authModeltree :roleBsnList="roleBsnList" @modifyMenuTree="modifyMenuTree"></ynet-authModeltree>
            </div>
            <div v-if="modifyVisible">
                <el-dialog title="Modify" :visible.sync="modifyVisible">
                    <el-form class="formEmbedTable">
                        <table> 
                                <tr>
                                    <td>Function：</td>
                                    <td colspan="3">
                                        <el-form-item>
                                        <span>{{modifyItem.bsnName}}</span>
                                        </el-form-item>
                                    </td>			
                                </tr>
                                
                                <tr>
                                    <td>Authorization Type：</td>
                                    <td colspan="3">
                                        <el-form-item >
                                            <el-select v-model="modifyItem.needAuth" placeholder="Authorization Type" clearable @change="changeNeedAuth">
                                                <el-option  v-for="item in needAuthList" :value="item.value" :label="item.label"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </td>	
                                </tr>
                                <tr>
                                    <td>Authorization Mode：</td>
                                    <td colspan="3">
                                        <el-form-item >
                                                <el-select v-model="modifyItem.authType" clearable :disabled="isAuthType">
                                                    <el-option v-for="item in authTypeList" :value="item.value" :label="item.label"></el-option>
                                                </el-select>
                                            </el-form-item>
                                    </td>	
                                </tr>
                            </table>
                    </el-form>
                    <div class="mb20 btnArea tac">
                        <el-button type="primary" @click=" getrule">Confirm</el-button>
                        <el-button type="primary" @click="modifyVisible=false,refreshPage()">Cancel</el-button>
                    </div>
                </el-dialog>
            </div>
            <!--授权确认页面弹窗-->
            <div>
                <ynet-authDialog ref="dialogTable"  @getAuthForm="getAuthForm"></ynet-authDialog>
            </div>
		</div>
	</ynet-content>
</template>

<script>
	//import authModelEdit from './components/authModelEdit';
	export default {
		name:'authModelMain',
		components:{
           //authModelEdit,
		},
        mounted(){
            this.refreshPage();
        },
		data(){
            return { 
                modifyVisible:false,
                dialogTableVisible:false,
                isAuthType:true,
                authType:'',
            	needAuth:'',
                needAuthList:[
                    {label:'Immediate Effect',value:'0'},
                    {label:'Mutual authorization',value:'1'},
                ],
                authTypeList:[
                    {label:'Counter Authorization',value:'1'},
                    {label:'Audits Authorization',value:'2'},
                ],
                roleBsnList:[],
                modifyItem:{
                    bsnCode:'',needAuth:'',authType:'',
                },
                tree:true,
                modify:false,
                authModelEdit:{
                    bsnImList:[{bsnCode:'',needAuth:'',authType:''}],bsnCode:'IM0104',actionType:'1',
                },
                authForm: { authUserNo: '', password: '',randomField:''},
                rules:{
                    //authUserNo: [{required: true, message: '用户名不能为空', trigger: 'blur' }],
					
                },
            }
            
        },
		methods:{ 
            getAuthForm(val){
                //获取授权数据
                this.authForm=val;
                //console.log("val:"+JSON.stringify(val));
                this.authModelEdit=$.extend(this.authModelEdit,this.authForm);
                this.submit()
            },
            getrule(){//提交前校验不能为空
                if(1==this.modifyItem.needAuth && 0==this.modifyItem.authType){
                    this.$alert('empty', 'warning', {
                        confirmButtonText: 'Confirm',
                    });
                }else{
                    this.queryAuth()
                }
            },
            submit(){
                this.authModelEdit.bsnImList[0].bsnCode=this.modifyItem.bsnCode;
                this.authModelEdit.bsnImList[0].needAuth=this.modifyItem.needAuth;
                this.authModelEdit.bsnImList[0].authType=this.modifyItem.authType;
                //this.authModelEdit=$.extend(this.authModelEdit,this.authForm)
                //console.log("authModelEdit:"+JSON.stringify(this.authModelEdit))
                this.$tools.request(this,"IM0104Action.do",this.authModelEdit).then(data=>{
                    //console.log("data:"+JSON.stringify(data))
                    if(data.body.errorCode==0){
                        this.modifyVisible =false 
                        if(this.needAuth==1 && this.authType==2){
                            this.$message({
								type:'success',
								message:'Waiting for Authorized',
							})
                            this.tree = true
                        }else if(this.needAuth==1 && this.authType==1){
                            if(data.body.authStatus==1){
                                this.$message.error({
                                     message:'Authorship No. or password wrong ,please resume load',
                                    showClose:true,
                                })
                                this.tree = true
                            }else{
                                this.$message({
                                    type:'success',
                                    message:'Modify Success',
                                    showClose:true,
                                })
                                this.tree = true
                            }
                        }else{
                            this.$message({
                                type:'success',
                                message:'Modify Success',
                                showClose:true,
                            })
                            this.tree = true
                        }
                    }else{
                        this.$alert("System Error Try again", "warning", {
                            confirmButtonText: 'Confirm',
                            callback: action => {
                                //this.refreshPage()
                                this.modifyVisible =false 
                                this.tree = true
                            }
                        });
                    }
                })
            },
            queryAuth(formName){//查询授权模式
                let body={};body.bsnCode="IM0104";body.version="IM0104",
                this.$tools.request(this,"IM010001Action.do",body).then(data=>{
                    //console.log("auth:"+JSON.stringify(data))
                    if(data.body.needAuth==0){//无需授权
                        this.submit()
                    }else if(data.body.needAuth==1 && data.body.authType==1){
                        this.needAuth=1;
                        this.authType=1;
                        this.$refs["dialogTable"].show()
                        // this.dialogTableVisible=true;
                    }else if( data.body.needAuth==1 && data.body.authType==2){
                        this.needAuth=1;
                        this.authType=2;
                        this.submit()
                    }else{
                        this.$alert('Authorization Type Error',  {
                            confirmButtonText: 'Confirm',
                            callback: action => {
                            }
                        });
                    }
                })
			},
            changeNeedAuth(val){//记录是否需要授权
                //console.log("val"+val)
                if(1==val){
                    this.modifyItem.authType="";
                     this.isAuthType=false;
                     
                }else{
                    this.isAuthType=true;
                    this.modifyItem.authType="";
                }
            },
            refreshPage(){
                this.tree=true;
                this.modifyVisible=false;
                this.roleBsnList=[];
                let bsnTemp=[];
                let bsnTempBak=[];
                this.$tools.request(this,"IM0104QueryAction.do").then(data=>{
                    if(data.body.errorCode==0){
                        let bsnAuthList=data.body.imBsnAuthList;
                        //console.log("bsnAuthList:"+JSON.stringify(bsnAuthList))
                        $.each(bsnAuthList,function(i,m){//一级菜单
                            if(m.parentBsnCode==undefined || m.parentBsnCode==""){
                                m.childrens=[]
                                bsnTemp.push(m)
                                //bsnAuthList.splice(i,1)
                            }
                        })
                        $.each(bsnTemp,function(i,m){
                            $.each(bsnAuthList,function(j,n){
                                if(n.parentBsnCode==m.bsnCode){
                                    //n.childrens=[]
                                    m.childrens.push(n)
                                }
                            })
                        })
                        $.each(bsnTemp,function(i,m){
                            $.each(m.childrens,function(j,n){
                                $.each(bsnAuthList,function(k,p){
                                    
                                    if(p.parentBsnCode==n.bsnCode){
                                        console.log(123)
                                        if(!n.childrens){
                                            n.childrens=[]
                                        }
                                        
                                        n.childrens.push(p);
                                    }
                                })
                            })
                        })
                        //console.log("bsnTemp:"+JSON.stringify(bsnTemp))
                        this.roleBsnList = bsnTemp
                    }else{
                        this.$alert("System Error Try again", "warning", {
                            confirmButtonText: 'confirm',
                            callback: action => {
                            }
                        });
                    }
                    
                })
            },
            backMenu(){
                this.modify=false;
                this.tree=true;
            },
            modifyMenuTree(val){

                this.modifyItem = val 
                //console.log("modifyItme:"+JSON.stringify(this.modifyItem))
                this.tree = false
                this.modifyVisible = true
                if(1==this.modifyItem.needAuth){
                     this.isAuthType=false;
                     
                }else{
                    this.isAuthType=true;
                    this.authType="";
                }
            },
		}
	}
</script>